﻿var $edittr = null;
var id = GetQueryString("Id");
$(function () {
    if (id != "") {
        IntData(id);
        GetCodeDataTypeSelectList();
        $("#add-parameter-form").validate({
            submitHandler: function () {
                var jsonobj = {};
                jsonobj.name = $("#add-parameter-Name").val();
                jsonobj.type = $("#add-parameter-DataTypeSelect").val();
                jsonobj.description = $("#add-parameter-Description").val().replace(",", "，");
                jsonobj.id = "";
                var tr = AssembleParameterData(jsonobj);
                $("#table-details-parameter").append(tr);
                $('#add-parameter-modal').modal('hide');
                ShowGritter("添加成功");
                console.log(JSON.stringify(tr));
            }
        });
        $("#update-parameter-form").validate({
            submitHandler: function () {
                var jsonobj = {};
                jsonobj.name = $("#update-parameter-Name").val();
                jsonobj.type = $("#update-parameter-DataTypeSelect").val();
                jsonobj.description = $("#update-parameter-Description").val().replace(",", "，");
                jsonobj.id = "";
                if ($edittr != null) {
                    $(AssembleParameterData(jsonobj)).replaceAll($edittr);
                }

                $('#update-parameter-modal').modal('hide');
                ShowGritter("编辑成功");
                console.log(JSON.stringify(jsonobj));
            }
        });

        $("#UpdateDataSource").validate({
            submitHandler: function () {
                try {
                    var parameterlist = $("#table-details-parameter tbody tr");
                    if (parameterlist.length > 0) {
                        var parstr = "";
                        for (var j = 0; j < parameterlist.length; j++) {
                            var tempjson1 = JSON.parse($(parameterlist[j]).attr("data"));
                            parstr += (tempjson1.name + ",");
                            parstr += (tempjson1.type + ",");
                            parstr += (tempjson1.description);
                            if (j != (parameterlist.length - 1)) {
                                parstr += ",";
                            }
                        }
                        $("#ParameterSettings").val(parstr);
                        //修改
                        var url = "PublicDataSourceManagement.html";
                        $.ajax({
                            url: apiUrl,
                            type: "Post",
                            dataType: "json",
                            data: $("#UpdateDataSource").serialize(),
                            success: function (data) {
                                if (data.IsSccuess) {
                                    ShowGritter("保存成功!", 2, url);
                                } else {
                                    if (IsLogined(data.ErrorCode)) {
                                        alert("失败（" + "错误代码：" + data.ErrorCode + "，错误信息：" + data.Message + ")");
                                    }
                                }
                            },
                            beforeSend: function () {
                                ShowGritter("正在提交");
                            }
                        });
                    }
                } catch (e) {
                    console.log(e);
                }

            }
        });
    }
});
//组合参数数据  编辑时需调用此方法处理返回的数据
//传入的参数类型为对象数组
var AppadParameterData = function (data) {
    //var list = new Array();
    var trlist = "";
    for (var i = 0; i < data.length; i++) {
        var $this = data[i];
        //list.push({ name: $this.Name, id: $this.Id, type: $this.Type, description: $this.Description });
        trlist += AssembleParameterData({ name: $this.Name, id: $this.Id, type: $this.Type, description: $this.Description });
    }
    $("#table-details-parameter").append(trlist);
}
//传入参数对象返回一行的表格数据
var AssembleParameterData = function (data) {
    var tr = "<tr data='" + JSON.stringify(data) + "'><td>";
    tr += (data.name + "</td>");
    tr += ("<td>" + data.type + "</td>");
    tr += ("<td>" + data.description + "</td>");//备注
    tr += ("<td>  <a href='javascript:void(0)' onclick=\"UpateParameterData(this)\"><i class='fa fa-pencil-square-o '></i>编辑</a>&nbsp;<a href='javascript:void(0)' onclick=\"DeleteData(this)\"><i class='fa  fa-times-circle'></i>删除</a></td></tr>");
    return tr;
}


//获取可用代码的select列表
var GetCodeDataTypeSelectList = function () {
    var list = GetCodeDataType();
    var str = "";

    for (var i = 0; i < list.length; i++) {
        str += ("<option value=\"" + list[i] + "\">" + list[i] + "</option>");
    }
    $("#add-parameter-DataTypeSelect").append(str);
    $("#update-parameter-DataTypeSelect").append(str);
    $("#add-column-DataTypeSelect").append(str);
    $("#update-column-DataTypeSelect").append(str);
}
//删除当前行数据
var DeleteData = function (obj) {
    if (confirm("是否删除？")) {
        $(obj).parent().parent().remove();
    }
}


//编辑传入参数按钮触发的操作
var UpateParameterData = function (obj) {
    var tr = $(obj).parent().parent();
    $edittr = tr;
    var json = tr.attr("data");
    var jsonobj = JSON.parse(json);
    $("#update-parameter-Name").val(jsonobj.name);
    $("#update-parameter-DataTypeSelect").val(jsonobj.type);
    $("#update-parameter-Description").val(jsonobj.description);
    $('#update-parameter-modal').modal({
        show: true
    });
}
//新增传入参数按钮触发的操作
var AddParameter = function () {
    document.getElementById('add-parameter-form').reset();
    $('#add-parameter-modal').modal({
        show: true
    });
}
//初始化数据
var IntData = function (sourceId) {
    $.ajax({
        url: apiUrl,
        dataType: "json",
        data: {
            "sys:sno": "PubDS",
            "sys:act": "GetDataSource",
            "Id": sourceId,
        },
        success: function (data) {
            if (data.IsSccuess) {
                $("#SourceName").val(data.Data.Name);
                $("#Description").val(data.Data.Description);
                $("#SourceValue").val(data.Data.Value);
                $("#SourceId").val(data.Data.Id);
                if (data.Data.Type == 1) {
                    $("#SourceType-1").attr("checked", "checked");
                } else {
                    $("#SourceType-2").attr("checked", "checked");
                }
                AppadParameterData(data.Data.Parameters);
            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert("失败（" + "错误代码：" + data.ErrorCode + "，错误信息：" + data.Message + ")");
                }
            }
        }
    });
}